﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lottery.Control {
    /// <summary>
    /// 文字 控件
    /// </summary>
    public partial class WZControl : UserControl {
        public WZControl() {
            InitializeComponent();

            editThree.Items.AddRange(new string[] { "大", "中", "小" });
            editOne.Items.AddRange(new string[] { "大", "中", "小" });
            editZero.Items.AddRange(new string[] { "大", "中", "小" });
            editPre.Items.AddRange(new string[] { "大", "中", "小" });

            menuItem1.Text = "大中小";
            menuItem2.Text = "大小中";
            menuItem3.Text = "小中大";
            menuItem4.Text = "中小大";
        }

        private int index;

        /// <summary>
        /// 场次
        /// </summary>
        public int Index {
            get { return index; }
            set {
                index = value;
                labelIndex.Text = string.Format("第 {0} 场", index);
            }
        }

        /// <summary>
        /// 3
        /// </summary>
        public string Three {
            get { return editThree.Text; }
            set { editThree.Text = value; }
        }

        /// <summary>
        /// 1
        /// </summary>
        public string One {
            get { return editOne.Text; }
            set { editOne.Text = value; }
        }

        /// <summary>
        /// 0
        /// </summary>
        public string Zero {
            get { return editZero.Text; }
            set { editZero.Text = value; }
        }

        /// <summary>
        /// 预测文字
        /// </summary>
        public string PreText {
            get { return editPre.Text; }
            set { editPre.Text = value; }
        }

        /// <summary>
        /// 数据是否完整
        /// </summary>
        public bool IsOK {
            get {
                return Three != string.Empty && One != string.Empty && Zero != string.Empty && PreText != string.Empty;
            }
        }

        /// <summary>
        /// 输入的数据是否正确
        /// </summary>
        public bool InputIsOK {
            get {
                var result = IsOK;
                if (!result) {
                    return false;
                }

                return (Three != One && Three != Zero && One != Zero);
            }
        }

        private System.Windows.Forms.Control jumpToNext;
        /// <summary>
        /// 按回车键的时候，焦点跳转到的控件
        /// </summary>
        public System.Windows.Forms.Control JumpToNext {
            get { return jumpToNext; }
            set {
                jumpToNext = value;
                editPre.JumpToNext = value;
            }
        }

        public void Clear() {
            editOne.SelectedIndex = -1;
            editThree.SelectedIndex = -1;
            editZero.SelectedIndex = -1;
            editPre.SelectedIndex = -1;
        }


        private void ChangeEdit(ComboEdit edit) {
            if (edit.Items.Count == 0) {
                return;
            }

            var index = edit.SelectedIndex;
            if (edit.Items[0].ToString() == "大") {
                edit.Items.Clear();
                edit.Items.AddRange(new string[] { "首", "次", "末" });
                edit.SelectedIndex = index;
            } else {
                edit.Items.Clear();
                edit.Items.AddRange(new string[] { "大", "中", "小" });
                edit.SelectedIndex = index;
            }
        }

        private string m_text = "大中小";

        /// <summary>
        /// 设置显示的文本。在 '大中小' 与 '首次末' 之间切换
        /// </summary>
        /// <param name="text"></param>
        public void SetText(string text) {
            if (m_text == text) return;

            m_text = text;
            ChangeEdit(editThree);
            ChangeEdit(editOne);
            ChangeEdit(editZero);
            ChangeEdit(editPre);
            if (m_text == "大中小") {
                menuItem1.Text = "大中小";
                menuItem2.Text = "大小中";
                menuItem3.Text = "小中大";
                menuItem4.Text = "中小大";
            } else {
                menuItem1.Text = "首次末";
                menuItem2.Text = "首末次";
                menuItem3.Text = "末次首";
                menuItem4.Text = "次末首";
            }
        }

        private void WZControl_EnabledChanged(object sender, EventArgs e) {
            label2.Visible = label3.Visible = label4.Visible = this.Enabled;
            editThree.Visible = editOne.Visible = editZero.Visible = this.Enabled;
            label1.Visible = this.Enabled;
            editPre.Visible = this.Enabled;
        }

        public void FocusOnEmptyEdit() {
            if (editThree.Text == string.Empty)
                editThree.Focus();
            else if (editOne.Text == string.Empty)
                editOne.Focus();
            else if (editZero.Text == string.Empty)
                editZero.Focus();
            else if (editPre.Text == string.Empty)
                editPre.Focus();
            else
                editThree.Focus();
        }

        public void SetDefaultValue() {
            editThree.SelectedIndex = 0;
            editOne.SelectedIndex = 1;
            editZero.SelectedIndex = 2;
        }

        private void menuItem1_Click(object sender, EventArgs e) {
            // 首次末
            editThree.SelectedIndex = 0;
            editOne.SelectedIndex = 1;
            editZero.SelectedIndex = 2;
        }

        private void menuItem2_Click(object sender, EventArgs e) {
            // 首末次
            editThree.SelectedIndex = 0;
            editOne.SelectedIndex = 2;
            editZero.SelectedIndex = 1;
        }

        private void menuItem3_Click(object sender, EventArgs e) {
            // 末次首
            editThree.SelectedIndex = 2;
            editOne.SelectedIndex = 1;
            editZero.SelectedIndex = 0;
        }

        private void menuItem4_Click(object sender, EventArgs e) {
            // 次末首
            editThree.SelectedIndex = 1;
            editOne.SelectedIndex = 2;
            editZero.SelectedIndex = 0;
        }
    }
}
